package com.orders.activity;

import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import  com.orders.util.DrawQRActivity;

import com.orders.util.MipcaActivityCapture;

import com.orders.R;
import com.orders.http.HttpUtil;


public class UserinfoActivity extends Activity {


	private SharedPreferences mSharedPreferences;

	private SharedPreferences.Editor mEditor;

	private TextView userinfo_username;
	private TextView userinfo_phone;
	private TextView userinfo_renshu;
	private TextView userinfo_createtime;
	private TextView userinfo_bianhao;
	private TextView userinfo_total;
	private final static int TAKECONFIRM_CODE = 10;
	private String username;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.userinfo);

		mSharedPreferences = getSharedPreferences("SharedPreferences", Context.MODE_PRIVATE);

		username = mSharedPreferences.getString("username", null);

		userinfo_username = (TextView)findViewById(R.id.userinfo_username);
		userinfo_phone = (TextView)findViewById(R.id.userinfo_phone);
		userinfo_renshu = (TextView)findViewById(R.id.userinfo_renshu);
		userinfo_createtime = (TextView)findViewById(R.id.userinfo_createtime);
		userinfo_bianhao = (TextView)findViewById(R.id.userinfo_bianhao);
		userinfo_total = (TextView)findViewById(R.id.userinfo_total);



	//	initData();

	}

	void initData() {
		Map<String, String> parameter = new HashMap<String, String>();
		parameter.put("username", username);


		String result = HttpUtil.sendRequest(HttpUtil.userdetails, parameter);

		HttpUtil.sendRequest(HttpUtil.canzhuoupdatestatus, parameter);
		
		Toast.makeText(this, "付款成功,5秒后退出", Toast.LENGTH_SHORT).show();
		try {

			JSONArray tables = new JSONArray(result);

			for (int i = 0; i < tables.length(); i++) {

				JSONObject obj_tmp = tables.getJSONObject(i);

				userinfo_username.setText(obj_tmp.getString("username"));
				userinfo_phone.setText(obj_tmp.getString("phone"));
				userinfo_renshu.setText(obj_tmp.getString("renshu"));
				userinfo_createtime.setText(obj_tmp.getString("createtime"));
				userinfo_bianhao.setText(obj_tmp.getString("bianhao"));
				userinfo_total.setText(obj_tmp.getString("total")+"(元)");


			}
			
			   new Timer().schedule(new TimerTask() {
					@Override
					public void run() {
						
					quit_out();
					}
				}, 5000);

		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Log.e("mobile", "格式转换错误");
		}

	}


	@Override
	protected void onRestart() {
		super.onRestart();
		//initData();

	}


	//结束用餐按钮响应函数
	public void loginout_system(View v){
		
		
		Intent intent = new Intent();
		intent.setClass(UserinfoActivity.this, MipcaActivityCapture.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		startActivityForResult(intent, TAKECONFIRM_CODE);
		
		/*
		
		Intent intent = new Intent(this, DrawQRActivity.class);
		Bundle bundle = new Bundle();
	
		bundle.putString("ciphertext", "1");
		intent.putExtras(bundle);
		startActivity(intent);
		*/
		
	}
	
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
		case TAKECONFIRM_CODE:
			if (resultCode == RESULT_OK) {
			//	Bundle bundle = data.getExtras();
			String	text = data.getStringExtra("result");
			username=text;
			
			initData();
			//	Toast.makeText(this, text, Toast.LENGTH_SHORT).show();

				} else {
					Toast.makeText(this, "请再次扫描", Toast.LENGTH_SHORT).show();
				}
			break;
			}
		
		}

   
	

	
	private void quit_out(){
		Map<String, String> parameter = new HashMap<String, String>();
		parameter.put("username", username);

	//	HttpUtil.sendRequest(HttpUtil.canzhuoupdatestatus, parameter);
		
		Intent intent = new Intent(UserinfoActivity.this, LoginActivity.class);
		startActivity(intent);

		this.finish();
		
		

		mSharedPreferences = getSharedPreferences("SharedPreferences",
				Context.MODE_PRIVATE);

		mEditor = mSharedPreferences.edit();
		mEditor.putString("username", null);
		mEditor.commit();	

		
	}

}
